#include "loginwidget.h"

LoginWidget::LoginWidget(QWidget *parent)
    : QWidget(parent)
{
    // 设置窗口标题和大小
    setWindowTitle(tr("登录系统"));
    resize(400, 300);

    // 创建用户名输入框
    QLabel *usernameLabel = new QLabel(tr("用户名:"), this);
    m_usernameEdit = new QLineEdit(this);
    m_usernameEdit->setPlaceholderText(tr("请输入用户名"));

    // 创建密码输入框
    QLabel *passwordLabel = new QLabel(tr("密码:"), this);
    m_passwordEdit = new QLineEdit(this);
    m_passwordEdit->setEchoMode(QLineEdit::Password);
    m_passwordEdit->setPlaceholderText(tr("请输入密码"));

    // 创建验证码输入框和验证码显示区域
    QLabel *codeLabel = new QLabel(tr("验证码:"), this);
    m_codeEdit = new QLineEdit(this);
    m_codeEdit->setPlaceholderText(tr("请输入验证码"));
    m_verificationCode = new VerificationCode(this);

    // 创建刷新验证码按钮
    m_refreshButton = new QPushButton(tr("刷新"), this);
    connect(m_refreshButton, &QPushButton::clicked, this, &LoginWidget::refreshVerificationCode);

    // 创建登录按钮
    m_loginButton = new QPushButton(tr("登录"), this);
    connect(m_loginButton, &QPushButton::clicked, this, &LoginWidget::onLoginButtonClicked);

    // 点击验证码区域也可以刷新验证码
    connect(m_verificationCode, &VerificationCode::clicked, this, &LoginWidget::refreshVerificationCode);

    // 布局
    QHBoxLayout *usernameLayout = new QHBoxLayout();
    usernameLayout->addWidget(usernameLabel);
    usernameLayout->addWidget(m_usernameEdit);

    QHBoxLayout *passwordLayout = new QHBoxLayout();
    passwordLayout->addWidget(passwordLabel);
    passwordLayout->addWidget(m_passwordEdit);

    QHBoxLayout *codeLayout = new QHBoxLayout();
    codeLayout->addWidget(codeLabel);
    codeLayout->addWidget(m_codeEdit);
    codeLayout->addWidget(m_verificationCode);
    codeLayout->addWidget(m_refreshButton);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addLayout(usernameLayout);
    mainLayout->addLayout(passwordLayout);
    mainLayout->addLayout(codeLayout);
    mainLayout->addWidget(m_loginButton);

    // 设置布局间距和边距
    mainLayout->setSpacing(15);
    mainLayout->setContentsMargins(30, 30, 30, 30);

    // 设置Tab顺序
    setTabOrder(m_usernameEdit, m_passwordEdit);
    setTabOrder(m_passwordEdit, m_codeEdit);
    setTabOrder(m_codeEdit, m_loginButton);
}

LoginWidget::~LoginWidget()
{
}

void LoginWidget::onLoginButtonClicked()
{
    QString username = m_usernameEdit->text().trimmed();
    QString password = m_passwordEdit->text();
    QString code = m_codeEdit->text().trimmed();

    // 检查是否有空字段
    if (username.isEmpty() || password.isEmpty() || code.isEmpty()) {
        QMessageBox::warning(this, tr("警告"), tr("请填写所有字段！"));
        refreshVerificationCode();
        return;
    }

    // 验证用户名和密码
    bool usernameCorrect = (username == VALID_USERNAME);
    bool passwordCorrect = (password == VALID_PASSWORD);
    bool codeCorrect = (code.toLower() == m_verificationCode->getCode().toLower());

    // 如果所有验证都通过
    if (usernameCorrect && passwordCorrect && codeCorrect) {
        QMessageBox::information(this, tr("成功"), tr("登录成功！"));
        // 这里可以添加登录成功后的操作，比如打开主窗口
    } else {
        // 显示错误信息
        QString errorMsg;
        if (!usernameCorrect) {
            errorMsg += tr("用户名错误！\n");
        }
        if (!passwordCorrect) {
            errorMsg += tr("密码错误！\n");
        }
        if (!codeCorrect) {
            errorMsg += tr("验证码错误！\n");
        }

        QMessageBox::warning(this, tr("错误"), errorMsg);

        // 刷新验证码
        refreshVerificationCode();

        // 清空验证码输入框
        m_codeEdit->clear();

        // 如果用户名或密码错误，将焦点设置到相应字段
        if (!usernameCorrect) {
            m_usernameEdit->setFocus();
        } else if (!passwordCorrect) {
            m_passwordEdit->setFocus();
        } else {
            m_codeEdit->setFocus();
        }
    }
}

void LoginWidget::refreshVerificationCode()
{
    m_verificationCode->generateNewCode();
    m_codeEdit->clear();
    m_codeEdit->setFocus();
}
